package com.yourenbang.lingyun.transaction.web.controller.pay.withdraw;

import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON;
import com.yourenbang.lingyun.constants.enums.pay.PayTypeEnum;
import com.yourenbang.lingyun.core.base.controller.BaseController;
import com.yourenbang.lingyun.core.base.interceptor.ExcludeInterceptor;
import com.yourenbang.lingyun.transaction.web.service.pay.withdraw.WithdrawService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * 资金代付
 * <p>
 * 2023.11.16 增加灵会算代付通道
 */
@Slf4j
@RestController
@RequestMapping("/transaction/fund/withdraw/channel")
@CrossOrigin("*")
public class WithdrawChannelController extends BaseController {

    @Autowired
    private WithdrawService withdrawService;

    static final String RESPONSE_SUCCESS = "success";
    static final String RESPONSE_FAIL = "fail";


    /**
     * 灵会算提现回调
     *
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/1962">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/linghuisuan/withdraw/notice")
    public String withdrawNotice(HttpServletRequest httpServletRequest) {

        String requestBody = getBodyContent(httpServletRequest);
        if (false) {
            //验签
        }

        boolean isOk = withdrawService.fundPayNoticeBody(PayTypeEnum.LINGHUISUAN.getCode(), requestBody);
        return isOk ? RESPONSE_SUCCESS : RESPONSE_FAIL;

    }


    /**
     * 灵会算签约回调
     *
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/1962">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/linghuisuan/sign/notice")
    public String signNotice(@RequestBody String param) {
        withdrawService.withdrawSign(param);
        return RESPONSE_SUCCESS;
    }

    /**
     * 云会算提现回调
     *
     * @see <a href="https://huoyan.kaolakuaishou.cn/project/97/interface/api/1962">火眼接口连接</a>
     */
    @ExcludeInterceptor
    @PostMapping(value = "/yunhuisuan/withdraw/notice")
    public String yunhuisuanWithdrawNotice(HttpServletRequest httpServletRequest) {

        String requestBody = getBodyContent(httpServletRequest);

        boolean isOk = withdrawService.fundPayNoticeBody(PayTypeEnum.YUNHUISUAN.getCode(), requestBody);

        Map<String, String> jsonReturn = new HashMap<>(1);
        jsonReturn.put("handled", isOk ? RESPONSE_SUCCESS.toUpperCase() : RESPONSE_FAIL.toUpperCase());

        return JSON.toJSONString(jsonReturn);

    }


    /**
     * //todo 临时办法
     *
     * @param httpServletRequest
     * @return
     */
    private String getBodyContent(HttpServletRequest httpServletRequest) {

        try {
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = null;
            InputStream inputStream = httpServletRequest.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            String body = sb.toString();
            return body;
        } catch (Exception e) {
            log.error("parseParam error.", e);
        }
        return null;
    }

}
